package sma.iss;

import java.io.IOException;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DynamicModelRun {

	private static double YAW = 0.0;

	/** Logger for this class. */
	protected final static Logger LOGGER = LoggerFactory.getLogger(DynamicModelRun.class);

	/** Example test cases. */
	public final static double[] ALL_BETA_EXAMPLE = new double[] { 70, -72 };

	/** All values of beta to be tested, Provisional test cases. */
	public final static double[] ALL_BETA_PROV = new double[] { 72, 74, -70, -74 };
	/** All values of beta for system tests. */
	public final static double[] ALL_SYSTEM_BETA = new double[] { 71, 73, 75, -71, -73, -75 };

	@Test
	public void runBuildDynamicModel() {
		double[] allBetas = new double[] { -74 };
		for (double beta : allBetas) {
			try {
				runBuildDynamicModel(beta);
			} catch (Exception e) {
				LOGGER.error("Error with beta " + beta, e);
			}
		}
	}

	/** Explore the values of yaw angle. */
	@Test
	public void scanYawAngle() {
		for (int i = 0; i <= 7; i++) {
			YAW = i;
			try {
				runBuildDynamicModel(-74);
			} catch (Exception e) {
				LOGGER.error("Error with YAW " + YAW, e);
			}
		}
	}

	public void runBuildDynamicModel(double beta) throws IOException {
		LOGGER.info("runBuildDynamicModel");
		long startTime = System.currentTimeMillis();
		DynamicModel dynamicModel = new DynamicModel();

		dynamicModel.setBeta(beta);
		dynamicModel.setYaw(YAW);

		// because of speed of MAX_SARJ_SPEED = 0.15 you need 20 min to find the best position
		// dynamicModel.calculatePosition(-29);
		// dynamicModel.calculatePosition(-20);
		// dynamicModel.calculatePosition(-19);
		// dynamicModel.calculatePosition(-14);
		dynamicModel.calculatePosition(-10);
		// dynamicModel.calculatePosition(-4);
		// dynamicModel.calculatePosition(-2);
		// dynamicModel.calculatePosition(-1);
		dynamicModel.calculatePosition(0);
		// dynamicModel.calculatePosition(1);
		// dynamicModel.calculatePosition(4);
		// dynamicModel.calculatePosition(10);
		// max min in step is 12 (because speed of 0.25)
		int mnPerStep = 10;
		int nbMaxstep = 0;
		// int nbMaxstep = 4 + 1;
		for (int minute = mnPerStep; minute < nbMaxstep; minute += mnPerStep) {
			dynamicModel.calculatePosition(minute);
		}
		LOGGER.info("All positions={}", dynamicModel.getPositions());
		dynamicModel.saveResultInCsv("minute PerStep=" + mnPerStep);
		long timeTaken = System.currentTimeMillis() - startTime;
		LOGGER.info("end in {} ms, {} calculations (average {} ms)", timeTaken, dynamicModel.nbCaculationDone, timeTaken
				/ dynamicModel.nbCaculationDone.get());
	}

}
